package com.zhoug.chart3.markers;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.List;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author: zhoug
 * @Date: 2024-02-01
 * @Description:
 */
abstract class MPBaseRecyclerViewAdapter<DT> extends RecyclerView.Adapter<MPBaseViewHolder> {

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    /**
     * 数据集合
     */
    private List<DT> mData;
    private RecyclerView mRecyclerView;


    @LayoutRes
    protected abstract int getLayoutId(int viewType);

    protected abstract void onBindData(MPBaseViewHolder holder, DT data, int position, int viewType);


    public MPBaseRecyclerViewAdapter() {

    }

    public MPBaseRecyclerViewAdapter(Context context) {
        this.mContext = context;
    }

    /**
     * 获取数据
     *
     * @return
     */
    public List<DT> getData() {
        return mData;
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public void setData(List<DT> data) {
        this.mData = data;
    }


    /**
     * 获取 mData 的数目
     *
     * @return
     */
    protected int getDataSize() {
        return mData == null ? 0 : mData.size();
    }


    /**
     * 获取附加的列表
     *
     * @return
     */
    public RecyclerView getAttachedRecyclerView() {
        return mRecyclerView;
    }

    /**
     * 获取指定position位置的数据
     *
     * @param position
     * @return
     */
    public DT getItemData(int position) {
        int dataSize = getDataSize();
        if (dataSize > 0 && position < dataSize) {
            return mData.get(position);
        }
        return null;
    }


    @Override
    public int getItemCount() {
        return getDataSize();
    }


    @Override
    public int getItemViewType(int position) {
        return 100;
    }

    @NonNull
    @Override
    public MPBaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mLayoutInflater == null) {
            mLayoutInflater = LayoutInflater.from(parent.getContext());
        }
        return new MPBaseViewHolder(mLayoutInflater.inflate(getLayoutId(viewType), parent, false), this);

    }

    @Override
    public void onBindViewHolder(@NonNull MPBaseViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        onBindData(holder, getItemData(position), position, viewType);
    }

    /**
     * 获取 Context
     *
     * @return
     */
    protected final Context getContext() {
        return mContext;
    }

    /**
     * 设置Context
     *
     * @param context
     */
    public final void setContext(Context context) {
        this.mContext = context;
    }

    /**
     * adapter绑定RecyclerView时调用
     *
     * @param recyclerView
     */
    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.mRecyclerView = recyclerView;
    }


}
